﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
using TicketSellingServer;

namespace FlightSearchServer
{
    [ServiceContract]
    public interface IFlightSearchServiceClients
    {
        [WebGet(UriTemplate = "tickets?date={date}&src={source}&dst={destination}" )]
        [OperationContract]
        TicketList SearchTickets(string date, string source, string destination);

        [WebInvoke(Method="POST", BodyStyle=WebMessageBodyStyle.Wrapped, UriTemplate = "reservations/{seller}")]
        [OperationContract]
        string CreateReservation(string seller, string flight, string date);

        [WebInvoke(Method = "DELETE", UriTemplate = "reservations/{seller}/{id}")]
        [OperationContract]
        void CancelReservation(string seller, string id);
    }

    [DataContract]
    public class Ticket : IComparable
    {

        [DataMember]
        public Uri Id { get; set; }

        [DataMember]
        public String Company { get; set; }

        [DataMember]
        public String Date { get; set; }

        [DataMember]
        public String Flight { get; set; }

        [DataMember]
        public String Seats { get; set; }

        [DataMember]
        public String Price { get; set; }

        public Ticket() {}
        public Ticket(SellerFlightInfo ticket)
        {
            Company = ticket.Company;
            Date = ticket.Date;
            Flight = ticket.Flight;
            Seats = ticket.Seats;
            Price = ticket.Price;
        }

        public int CompareTo(object obj)
        {
            try
            {
                Ticket other = (Ticket)obj;
                if (int.Parse(this.Price) > int.Parse(other.Price))
                {
                    return 1;
                }

                if (int.Parse(this.Price) < int.Parse(other.Price))
                {
                    return -1;
                }

                //prices are equal

                if (int.Parse(this.Seats) < int.Parse(other.Seats))
                {
                    return 1;
                }

                if (int.Parse(this.Seats) > int.Parse(other.Seats))
                {
                    return -1;
                }

                //seats are equal too

                return string.Compare(this.Flight, other.Flight);
            }
            catch (Exception)
            {
                return 0;
            }
        }
    }

    [CollectionDataContract]
    public class TicketList : List<Ticket>
    {   
        public TicketList(){}
        public TicketList(List<Ticket> tickets) : base(tickets) {}
        public TicketList(SellerFlightsList tickets)
        {
            foreach (var ticket in tickets)
            {
                this.Add(new Ticket(ticket));
            }
        }
    }
}


